from typing import Optional

from model.uml_element import UmlElement
from model.uml_property import UmlProperty
from model.uml_generalization import UmlGeneralization
from model.uml_xmi_info import UmlXmiInfo


class UmlStereotype(UmlElement):
    def __init__(self) -> None:
        super().__init__(UmlXmiInfo.type_stereotype)
        self.name: str = ""
        self.generalizations: list[UmlGeneralization] = []
        self.ownedAttribute: Optional[UmlProperty] = None

    def to_json(self) -> dict[str, object]:
        json: dict = super().to_json()

        json["name"] = self.name

        item = []
        for value in self.generalizations:
            item.append(value.to_json())
        json["generalizations"] = item

        if self.ownedAttribute is not None:
            json["ownedAttribute"] = self.ownedAttribute.to_json()

        return json

    def from_json(self, json: dict):
        if json is None:
            return

        super().from_json(json)

        self.ownedAttribute = json.get("ownedAttribute", None)
